﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Threading;
using System.Threading.Tasks;

namespace WpfBeyond.pages
{
    /// <summary>
    /// PageTask.xaml 的交互逻辑
    /// </summary>
    public partial class PageTask : Page
    {
        private DispatcherTimer _Timer = null;

        public PageTask()
        {
            InitializeComponent();

            play_mode_list p_play_mode_list = new play_mode_list(cmb_search_type);

            if (_Timer == null)
            {
                _Timer = new DispatcherTimer();//定时器 ;
                _Timer.Tick += new EventHandler(timeCycle);
                _Timer.Interval = new TimeSpan(0, 0, 0, 0, 1000);
            }

            _Timer.Start();


        }

        public void timeCycle(object sender, EventArgs e)
        {
            try
            {
                if (TaskFactory.IsReflash() == true || Common.reflash_task == true)
                {
                    Common.reflash_task = false;

                    grid_task.ItemsSource = null;
                    grid_task.ItemsSource = TaskFactory.list;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void BtnStop_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;

            if (btn == null) return;

            btn.IsEnabled = false;

            string ebmid = btn.Tag.ToString();//获取需要停止的TASK 的EBMID

            Task.Run(() =>
            {
                Common.ShowLoading(true);

                try
                {
                    TaskInfo t = TaskFactory.GetTaskInfo(ebmid);

                    if (t == null) return;

                    t.Stop();//停止任务lbl_device

                    TaskFactory.Remove_TaskInfo(t);

                    Common.reflash_task = true;
                }
                catch (Exception ex)
                {
                    LogHelp.Error(ex.ToString());

                    btn.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() => { btn.IsEnabled = true; }));
                }

                Common.ShowLoading(false);
            });

        }

        private void BtnTaskDetail_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;

            string ebmid = btn.Tag.ToString();

            TaskInfo t = TaskFactory.GetTaskInfo(ebmid);
            if (t == null)
            {
                Common.ShowMessage("提示", "任务信息错误！");
                return;
            }
            WindowTaskDetail w = new WindowTaskDetail(t);
            w.Show();

        }

        Process pro_listen = null;//监听进程

        private void BtnPlay_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;

            string ebmid = btn.Tag.ToString();

            Task.Run(() =>
            {
                Common.ShowLoading(true);

                TaskInfo t = TaskFactory.GetTaskInfo(ebmid);
                string rtp = t.Rtp;

                if (t.IsMonitor == false)
                {
                    pro_listen = new Process();

                    //是否使用操作系统shell启动
                    pro_listen.StartInfo.UseShellExecute = false;
                    pro_listen.StartInfo.ErrorDialog = false;
                    //把外部程序错误输出写到StandardError流中(这个一定要注意,FFMPEG的所有输出信息,都为错误输出流
                    pro_listen.StartInfo.RedirectStandardError = true;
                    pro_listen.StartInfo.CreateNoWindow = true;//不显示程序窗口
                    pro_listen.StartInfo.FileName = @"ffplay.exe";

                    pro_listen.StartInfo.Arguments = "-nodisp -protocol_whitelist file,udp,rtp " + rtp;
                    pro_listen.Start();

                    t.IsMonitor = true;

                    pro_listen.BeginErrorReadLine();//开始异步读取 
                }
                else
                {
                    try
                    {
                        if (pro_listen != null)
                        {
                            if (pro_listen.HasExited == false)
                            {
                                pro_listen.Kill();
                                pro_listen.WaitForExit();
                                pro_listen.Close();
                                pro_listen.Dispose();
                            }
                        }

                        t.IsMonitor = false;
                    }
                    catch (Exception ex)
                    {
                        LogHelp.Error(ex.ToString());
                    }
                }

                Common.ShowLoading(false);
            });

        }

    }
}
